<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>点赞功能示例</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            color: #333;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        h1 {
            margin-bottom: 20px;
        }

        #likeButton {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
        }

        #likeButton:hover {
            background-color: #0056b3;
        }
        
        ul {
            list-style-type: none;
            padding: 0;
        }

        ul li {
            background: #f8f9fa;
            margin: 5px 0;
            padding: 10px;
            border-radius: 5px;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>内容标题</h1>
    <p>点赞数: <span id="likeCount">0</span></p>
    <button id="likeButton">点赞</button>
    <h2>我的点赞记录</h2>
    <ul id="userLikes"></ul>
</div>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const contentId = 1; // 假设内容ID为1
        const userId = 1; // 假设用户ID为1
        const likeButton = document.getElementById('likeButton');
        const likeCount = document.getElementById('likeCount');
        const userLikes = document.getElementById('userLikes');

        // 获取点赞数
        function fetchLikeCount() {
            const params = new URLSearchParams({
                contentId:contentId
            })
            fetch(`api/like/getLikeCount?${params.toString()}`)
                .then(response => response.json())
                .then(data => {
                    likeCount.textContent = data.data.count;
                    likeButton.textContent = data.data.count > 0 ? '取消点赞' : '点赞';
                });
        }

        // 获取用户点赞记录
        function fetchUserLikes() {
            const params = new URLSearchParams({
                userId:userId
            })
            fetch(`api/like/getUserLikes?${params.toString()}`)
                .then(response => response.json())
                .then(data => {
                    userLikes.innerHTML = data.data.likes.map(like => `<li>内容ID: ${like}</li>`).join('');
                });
        }

        // 点赞或取消点赞
        likeButton.addEventListener('click', function () {
            const headers = new Headers()
            headers.append('content-type', 'application/json')
            fetch(`api/like/like`, { method: 'POST',headers,body: JSON.stringify({ contentId: contentId, userId: userId }) })
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        fetchLikeCount();
                        fetchUserLikes();
                    }
                });
        });

        // 初始化页面
        fetchLikeCount();
        fetchUserLikes();
    });
</script>
</body>
</html>